package main;

import javax.swing.JEditorPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Position;

import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * 
 * @author Daniel Collins
 * @author Balazs Pete
 *
 */
public class TextArea extends JEditorPane {
	String currentText = "";
	private int lineCount;
	private JTextArea ta = new JTextArea();
	private LineNumberUpdater lineNumberUpdater = null;
	
 //  @jve:decl-index=0:
	
	/**
	 * This method initializes 
	 * 
	 * @param width width of text area
	 * @param height height of text area
	 */
	public TextArea(int width, int height) {
		super();
		initialize(width,height);

		// Listener for the Text Area
		KeyListener kl =  new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
			}

			@Override
			//each time a key is released, the following is executed...
			public void keyReleased(KeyEvent e) {
				updateLineNumbers();
			}

			@Override
			public void keyTyped(KeyEvent e) {
			}
		};
		addKeyListener(kl);
	}
	
	/**
	 * Initializes a new text area with a string
	 * @param width width
	 * @param height height
	 * @param text text contained in area
	 */
	public TextArea(int width, int height, String text){
		new TextArea(width,height);
		setText(text);
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize(int width, int height) {
        this.setSize(width, height);
	}
	
	/**
	 * Updates the currentText variable with the contents of the text area
	 * (possible solution for file I/O and recovery)
	 * 
	 * @author Balazs Pete
	 */
	public void updateTextVariable(){
		currentText = this.getText();
		ta.setText(currentText);
		//Remove // to see result in console..
		//System.out.println(currentText);
		
	}
	
	public void updateTextPane(){
		setText(currentText);
	}
	
	/**
	 * Method to insert a string at the location of the cursor(caret?).
	 * Use this method to insert text on a press of a button (symbol toolbar, etc)
	 * 
	 * @param in text to be entered (note: spaces are not added automatically)
	 * @param space if true adds a space " " character before and after 'in'
	 * 
	 * @author Balazs Pete
	 */
	protected void insertText(String in,boolean space){
		int pos = getCaretPosition();
		
		if(space){
			in = " "+in+" ";
		}
		
		Document doc = getDocument();
		if (doc != null) {
			try {
				doc.insertString(pos, in, null);
			} catch (BadLocationException e){
				throw new IllegalArgumentException(e.getMessage());
			}
		}
		
		try {
			currentText = doc.getText(0, doc.getLength());
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		updateTextPane();
		
		if(space){
			setCaretPosition(pos+3);
		} else {
			setCaretPosition(pos+1);
		}
	}
	
	/**
	 * Return the current text in the text area
	 * @return current text
	 * @author Balazs Pete
	 */
	public String getCurrentText(){
		updateTextVariable();
		return currentText;
	}
	
	/**
	 * 
	 */
	private void updateLineNumbers(){
		//TODO Better algorithm to find line number required
		ta.setText(getText());
		if(lineCount!=ta.getLineCount()){
			lineCount = ta.getLineCount();
			lineNumberUpdater = new LineNumberUpdater(lineCount);
			lineNumberUpdater.start();
		}
	}
	
	/**
	 * Method to get the number of lines in the text pane
	 * @return
	 */
	public int getLineNumber(){
		//TODO algorithm to find the line number required, and implemented in listener
		
		return 0;
	}
	
	/**
	 * Method returning the start and end positions from where the text is displayed
	 * @return
	 */
	public int[] getShownSection(){
		//TODO
		return null;
	}
	
	/**
	 * Method to get the active line in the text pane
	 * @return
	 */
	public int getActiveLine(){
		//TODO
		return 0;
	}
	
}  //  @jve:decl-index=0:visual-constraint="99,17"
